/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.epos.datalogic.define.dao.sale;

import com.openbravo.data.loader.query.QBFParameter;
import e.odbo.data.dao.ViewDAO;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.serialize.DataRead;
import com.openbravo.data.loader.serialize.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.sqlbuilder.QBFBuilder;
import com.openbravo.data.loader.serialize.serializer.write.SerializerWriteBasic;
import com.openbravo.data.loader.sentence.StaticSentence;
import com.openbravo.bean.sales.FindTicketsInfo;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class FindTicketsInfoDAO extends ViewDAO<FindTicketsInfo>{

    public FindTicketsInfoDAO(I_Session s) {
        super(s);
    }

    public FindTicketsInfo readValues(DataRead dr,FindTicketsInfo obj) throws BasicException {
        if(obj==null)
        	obj=new FindTicketsInfo();
        obj.setTicketid(dr.getInt(1));
        obj.setTickettype(dr.getInt(2));
        obj.setDate(dr.getTimestamp(3));
        obj.setName(dr.getString(4));
        obj.setCustomer( dr.getString(5));
        obj.setTotal((dr.getObject(6) == null) ? 0.0 : dr.getDouble(6).doubleValue());
        return obj;
    }

    public List<FindTicketsInfo> list(Object filter) throws BasicException {
        return new StaticSentence(s, new QBFBuilder(
                "SELECT T.TICKETID, T.TICKETTYPE, R.DATENEW, P.NAME, C.NAME, SUM(PM.TOTAL) "
                + "FROM RECEIPTS R JOIN TICKETS T ON R.ID = T.ID LEFT OUTER JOIN PAYMENTS PM ON R.ID = PM.RECEIPT LEFT OUTER JOIN CUSTOMERS C ON C.ID = T.CUSTOMER LEFT OUTER JOIN PEOPLE P ON T.PERSON = P.ID "
                + "WHERE ?(QBF_FILTER) GROUP BY T.ID, T.TICKETID, T.TICKETTYPE, R.DATENEW, P.NAME, C.NAME ORDER BY R.DATENEW DESC, T.TICKETID",
                new String[]{"T.TICKETID", "T.TICKETTYPE", "PM.TOTAL", "R.DATENEW", "R.DATENEW", "P.NAME", "C.NAME"}),
                new SerializerWriteBasic(new Datas[]{Datas.OBJECT, Datas.INT, Datas.OBJECT, Datas.INT, Datas.OBJECT, Datas.DOUBLE, Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.TIMESTAMP, Datas.OBJECT, Datas.STRING, Datas.OBJECT, Datas.STRING}),
                this).list(filter);
    }

    @Override
    public Class getSuportClass() {
        return FindTicketsInfo.class;
    }

    @Override
    public List<FindTicketsInfo> query(QBFParameter filter) throws BasicException {
         return new StaticSentence(s, new QBFBuilder(
                "SELECT T.TICKETID, T.TICKETTYPE, R.DATENEW, P.NAME, C.NAME, SUM(PM.TOTAL) "
                        + "FROM RECEIPTS R JOIN TICKETS T ON R.ID = T.ID LEFT OUTER JOIN PAYMENTS PM ON R.ID = PM.RECEIPT LEFT OUTER JOIN CUSTOMERS C ON C.ID = T.CUSTOMER LEFT OUTER JOIN PEOPLE P ON T.PERSON = P.ID "
                        + "WHERE ?(QBF_FILTER) GROUP BY T.ID, T.TICKETID, T.TICKETTYPE, R.DATENEW, P.NAME, C.NAME ORDER BY R.DATENEW DESC, T.TICKETID"
                ), null,
                this).list(filter);
    }
}
